//
//  HJTopView.swift
//  HJWeiBo
//
//  Created by 黄珏 on 15/11/1.
//  Copyright © 2015年 黄珏. All rights reserved.
//

import UIKit

class HJTopView: UIView {
    
    var status: HJStatus? {
        didSet{
            if let iconUrl = status?.user?.profile_image_url {
                iconView.sd_setImageWithURL(NSURL(string: iconUrl))
            }
            
            nameLabel.text = status?.user?.name
            timeLabel.text = status?.created_at
//            sourceLabel.text = status?.source
            verifiedView.image = status?.user?.verifiedTypeImage
            memberView.image = status?.user?.mbrankImage
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func prepareUI() {
//        添加子空间
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
//        addSubview(sourceLabel)
        addSubview(verifiedView)
        addSubview(memberView)
        iconView.translatesAutoresizingMaskIntoConstraints = false
        nameLabel.translatesAutoresizingMaskIntoConstraints  = false
        timeLabel.translatesAutoresizingMaskIntoConstraints = false
//        sourceLabel.translatesAutoresizingMaskIntoConstraints = false
        verifiedView.translatesAutoresizingMaskIntoConstraints = false
        memberView.translatesAutoresizingMaskIntoConstraints = false
        
//        iconView添加约束
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 8))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 8))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
//        nameLabel添加约束
        addConstraint(NSLayoutConstraint(item: nameLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: nameLabel, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 8))
        nameLabel.sizeToFit()
//        timeLabel添加约束
        addConstraint(NSLayoutConstraint(item: timeLabel, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 8))
        addConstraint(NSLayoutConstraint(item: timeLabel, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        timeLabel.sizeToFit()
//        sourceLabel添加约束
//        addConstraint(NSLayoutConstraint(item: sourceLabel, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: timeLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 8))
//        addConstraint(NSLayoutConstraint(item: sourceLabel, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: timeLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
//        verifiedView添加约束
        addConstraint(NSLayoutConstraint(item: verifiedView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: verifiedView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        verifiedView.sizeToFit()
//        verifiedView添加约束
        addConstraint(NSLayoutConstraint(item: memberView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: nameLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 5))
        addConstraint(NSLayoutConstraint(item: memberView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: nameLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -3))
        memberView.sizeToFit()
    }
    
    // MARK: - 懒加载
    /// 用户头像
    private lazy var iconView = UIImageView()
    
    /// 用户名称
    private lazy var nameLabel: UILabel = UILabel(fonsize: 14, textColor: UIColor.darkGrayColor())
    
    /// 时间label
    private lazy var timeLabel: UILabel = UILabel(fonsize: 9, textColor: UIColor.orangeColor())
    
    /// 来源
    private lazy var sourceLabel: UILabel = UILabel(fonsize: 9, textColor: UIColor.lightGrayColor())
    
    /// 认证图标
    private lazy var verifiedView = UIImageView()
    
    /// 会员等级
    private lazy var memberView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
}
